
package com.gegao.practise.lanuchcount;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;

import com.gegao.practise.R;

public class BrowserAssociatedHelpNotSingle {

    public static BrowserAssociatedHelpNotSingle mInstance;
    private Activity mActivity;
    
    private EfficientReadTip mEfficientReadTip;

    public BrowserAssociatedHelpNotSingle(Context context) {
        mActivity = (Activity) context;
        if (true) {
            mEfficientReadTip = new EfficientReadTip(context, null);
            android.util.Log.d("effi", "new EfficientReadTip");
        }
    }
    
    abstract class HelpTip {
        protected Activity mActivity;
        protected View mTip;
        protected boolean mShowTip = true;

        protected SharedPreferences mSharedPref;
        protected String mPreferenceKey;
        protected int mResId;

        ViewGroup mParent;

        public HelpTip(Context context, int resId, ViewGroup parent, String preferenceKey) {
            mPreferenceKey = preferenceKey;
            mActivity = (Activity) context;
            mResId = resId;
            mParent = parent;
            mSharedPref = SharedPreferenceUtils.getSharedPreferences((Activity) context,
                    SharedPreferenceUtils.HELP_SHARED_FILE);
        }

        public boolean isShown() {
            return mTip != null && mTip.isShown();
        }

        public View getTip() {
            return mTip;
        }

        public void show() {
            if (!mShowTip) {
                return;
            }
            final WindowManager wm = (WindowManager) mActivity
                    .getSystemService(Context.WINDOW_SERVICE);
            if (mTip != null) {
                removeTip();
            }
            final View tip = LayoutInflater.from(mActivity).inflate(mResId, null);
            wm.addView(tip, getWindowManagerLayoutParams());
            View close = tip.findViewById(R.id.close);
            close.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    wm.removeView(tip);
                    remove();
                }
            });
        }

        private void removeTip() {
            // if (null == mParent) {
            // mWindowManager.removeView(mTip);
            // } else {
            // mParent.removeView(mTip);
            // }
        }

        public WindowManager.LayoutParams getWindowManagerLayoutParams() {
            WindowManager.LayoutParams params = new WindowManager.LayoutParams();

            params = new WindowManager.LayoutParams();
            params.width = LayoutParams.MATCH_PARENT;
            params.height = LayoutParams.MATCH_PARENT;
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION + 3;
            params.format = PixelFormat.RGBA_8888;

            if (mResId == 99) {

                params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                        | WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            }
            else {
                params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
            }
            return params;
        }

        public void update() {
            if (isShown()) {
                show();
            }
        }

        public boolean remove() {
            if (mTip != null) {
                mShowTip = false;
                removeTip();
                mTip = null;
                // resetWM();
                return true;
            }
            return false;
        }

        // private void resetWM() {
        // mWindowManager = null;
        // params = null;
        // }

    }

    private class EfficientReadTip extends HelpTip {
        float dismiss_x;
        float dismiss_y;

        public EfficientReadTip(Context context, ViewGroup parent) {
            super(context, R.layout.tip_efficient_read, parent,
                    SharedPreferenceUtils.SHOW_EFFICIENT_READ_TIP);
            dismiss_x = 50;
            dismiss_y = 50;
        }

        @Override
        public boolean remove() {
            return super.remove();
        }

        public void show(Activity ac) {
            if (!mShowTip) {
                return;
            }
            final WindowManager wm = (WindowManager) ac
                    .getSystemService(Context.WINDOW_SERVICE);

            final View tip = LayoutInflater.from(ac).inflate(mResId, null);
            wm.addView(tip, getWindowManagerLayoutParams());
            View close = tip.findViewById(R.id.close);
            tip.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (MotionEvent.ACTION_UP == event.getAction()) {
                        float x = event.getX();
                        float y = event.getY();
                        if ((x > 0 && x < dismiss_x) && (y > 0 && y < dismiss_y)) {
                            remove();
                        }
                    }
                    return false;
                }
            });
            close.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    wm.removeView(tip);
                    remove();
                }
            });
        }
    }

    public void showEfficientTip() {
        showEfficientTipAccordingUrl();
    }
    
    private void showEfficientTipAccordingUrl() {
        if (null != mEfficientReadTip) {
            mEfficientReadTip.show();
        }

    }

    public static Activity sActivity;

    public static void show() {
        final WindowManager wm = (WindowManager) sActivity
                .getSystemService(Context.WINDOW_SERVICE);
        View mTip = LayoutInflater.from(sActivity).inflate(R.layout.tip_efficient_read, null);
        final View tip = mTip;
        wm.addView(mTip, getWindowManagerLayoutParams());
        View close = tip.findViewById(R.id.close);
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                wm.removeView(tip);
            }
        });
    }

    private static WindowManager.LayoutParams getWindowManagerLayoutParams() {
        WindowManager.LayoutParams params = new WindowManager.LayoutParams();

        params = new WindowManager.LayoutParams();
        params.width = LayoutParams.MATCH_PARENT;
        params.height = LayoutParams.MATCH_PARENT;
        params.type = WindowManager.LayoutParams.TYPE_APPLICATION + 3;
        params.format = PixelFormat.RGBA_8888;

        params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

        return params;
    }

}
